package com.example.sachon;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Calendar;

import com.example.sachon.DetailContentListAdapter.ViewHolder;
import com.example.sachon.vo.BoardItem;
import com.example.sachon.vo.ChonInfo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class ChonSchduleListAdapter extends BaseAdapter {

	private Context context;
	private LayoutInflater inflater;
	private int viewResourceId;
	private ArrayList<BoardItem> data;
	// references to our images

	public ChonSchduleListAdapter(ArrayList<BoardItem> data, int viewResourceId,  Context  context){
		this.context = context;
		inflater = LayoutInflater.from(context);
		this.viewResourceId = viewResourceId;
		this.data = data;
	}

	public int getCount() {
		return data.size();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if(convertView == null){
			viewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.acctivity_schdule_context, null);
			
			viewHolder._schdule_yyyymm = (TextView) convertView
					.findViewById(R.id.schdule_yyyymm);
			viewHolder._schdule_day= (TextView) convertView
					.findViewById(R.id.schdule_day);
			
			viewHolder._schdule_dd= (TextView) convertView
					.findViewById(R.id.schdule_dd);
			
			viewHolder._schdule_title= (TextView) convertView
					.findViewById(R.id.schdule_title);
			
			convertView.setTag(viewHolder);

			
		}else{
			viewHolder = (ViewHolder)convertView.getTag();
		}
		
		String wr_dt = data.get(position).getWr_dt();
		
		data.get(position).setFrom_dt(wr_dt.substring(0, 4)+ wr_dt.substring(5, 7) +wr_dt.substring(8, 10));
		String t_date =  data.get(position).getFrom_dt();
		String weekday = "";
		String yyyy ="";
		String mm   = "";
		String dd    = "";
		if( t_date.length() == 8){
			 yyyy = t_date.substring(0,  4);
		     mm   = t_date.substring(4, 6);
			dd    = t_date.substring(6, 8);
			
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.YEAR, Integer.parseInt(yyyy));
			calendar.set(Calendar.MONTH, Integer.parseInt(mm)-1);
			calendar.set(Calendar.DATE, Integer.parseInt(dd));
			
			String[] dayOfWeek = {"일", "월" , "화", "수", "목", "금", "토" };
			weekday = dayOfWeek[calendar.get(Calendar.DAY_OF_WEEK)-1];
		}
		viewHolder._schdule_yyyymm.setText(yyyy+"년"+mm+"월" );
		viewHolder._schdule_day.setText(weekday);
		viewHolder._schdule_dd.setText(dd+"일");
		viewHolder._schdule_title.setText(data.get(position).getTitle());	
 	
		return convertView;
	}
	
	
	public final class ViewHolder {
		private TextView _schdule_yyyymm ;
		private TextView _schdule_day;
		private TextView _schdule_dd;
		private TextView _schdule_title;
	}

}
